package com.saifan.wyy_ov.ui.property;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.RequestBean;
import com.saifan.wyy_ov.data.bean.ServiceRecordBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.LabelView;
import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.DensityUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-13 10:07:02
 * @descrption 特约服务记录
 */

public class EngagedServiceRecordActivity extends BaseActivity {
    private Toolbar toolbar;
    private ListView listView;
    private int page = 0;
    private List<ServiceRecordBean> beans =  new ArrayList<>();
    private GetData getData = new GetData();
    private CommonAdapter<ServiceRecordBean> adapter;
    private RefreshLayout refreshLayout;
    private RequestBean requestBean;
    private View loadlay;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestBean = new RequestBean(getRoomInfoBean().getFKGX_ZJ(), page);
        setContentView();
        initAdapter();
        query();
    }


    public void setContentView() {
        setContentView(R.layout.activity_engaged_service_record);
        loadlay = findViewById(R.id.loading_lay);
        this.refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        this.listView = (ListView) findViewById(R.id.listView);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    }


    public void query() {
        requestBean.setPage(page);

        if (beans.isEmpty()) {
            loadlay.setVisibility(View.VISIBLE);
        }
        getData.getDataJson(this, Api.Customer.SPECIAL_SERVICE_INFO, requestBean, "", new ResponseData() {
            @Override
            public void onSuccess(String json) {
                List<ServiceRecordBean> list = new Gson().fromJson(json, new TypeToken<List<ServiceRecordBean>>() {
                }.getType());
                if (list != null && !list.isEmpty()) {
                    if (page == 0) {
                        beans = list;
                    } else {
                        beans.addAll(list);
                    }


                } else {
                    if (page >0) {
                        page--;
                        ToastUtil.showToast(EngagedServiceRecordActivity.this, "信息已全部显示完毕！");
                    } else {
                        ToastUtil.showToast(EngagedServiceRecordActivity.this, "暂无更多信息");
                    }

                }

                adapter.mDatas = beans;
                adapter.notifyDataSetChanged();
                loadlay.setVisibility(View.GONE);
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);


            }

            @Override
            public void onFailed(String str) {
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);
                loadlay.setVisibility(View.VISIBLE);
                if (page > 0) page--;

            }
        });


    }

    public void initAdapter() {

        adapter = new CommonAdapter<ServiceRecordBean>(this, beans, R.layout.engaged_service_record_item) {
            @Override
            public void convert(ViewHolderHelper helper, ServiceRecordBean item) {



                helper.setText(R.id.TYFW_FSSJ, StringUtil.returnShow(item.getTYFW_FSSJ()));
                helper.setText(R.id.TYFW_LXR, StringUtil.returnShow(item.getTYFW_LXR()));
                helper.setText(R.id.TYFW_FWXM, StringUtil.returnShow(item.getTYFW_FWXM()));
                helper.setText(R.id.TYFW_LR, StringUtil.returnShow(item.getTYFW_LR()));

                LinearLayout targetView = helper.getView(R.id.layout);
                int window_width_dp = getWindowManager().getDefaultDisplay().getWidth();
                int window_width_px = DensityUtil.px2dip(
                        EngagedServiceRecordActivity.this, window_width_dp) - 20;


                LabelView label = helper.getView(12345);


                if (label == null) {
                    label = new LabelView(EngagedServiceRecordActivity.this);
                    label.setId(12345);

                    label.setHeight(DensityUtil.dip2px(EngagedServiceRecordActivity.this, 40));
                    label.setWidth(DensityUtil.dip2px(EngagedServiceRecordActivity.this, 120));
                    label.setGravity(Gravity.BOTTOM | Gravity.CENTER);


                }



                if (!item.isTYFW_SFSL() && !StringUtil.isNullOrEmpty(item.getTYFW_BSLYY())) {
                    label.setBackgroundColor(getResources().getColor(R.color.lable1));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("未受理");
                    helper.getView(R.id.TYFW_BSLYY).setVisibility(View.VISIBLE);
                    helper.setText(R.id.TYFW_BSLYY, "未受理原因：" + StringUtil.returnShow(item.getTYFW_BSLYY()));
                }  else if (item.isTYFW_SFSL()) {
                    label.setBackgroundColor(getResources().getColor(R.color.lable3));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("已受理");
                    helper.getView(R.id.TYFW_BSLYY).setVisibility(View.GONE);
                }
                else   {
                    label.setBackgroundColor(getResources().getColor(R.color.lable2));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("审核中");
                    helper.getView(R.id.TYFW_BSLYY).setVisibility(View.GONE);
                }

            }

        };
        listView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                page = 0;
                refreshLayout.setRefreshing(true);
                query();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                // TODO Auto-generated method stub
                refreshLayout.setLoading(true);
                page++;
                query();

            }
        });


    }


}
